package com.wanhe.base;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Created by XieZaiWei on 2017/6/14.
 * popupwindow的基类
 */

public abstract class BasePop extends PopupWindow implements PopupWindow.OnDismissListener {
	protected View mContentView;
	protected FragmentActivity mActivity;

	public BasePop() {
		super();
	}

	public BasePop(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public BasePop(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BasePop(Context context) {
		super(context);
	}

	public BasePop(int width, int height) {
		super(width, height);
	}

	public BasePop(View contentView, int width, int height, boolean focusable) {
		super(contentView, width, height, focusable);
	}

	public BasePop(FragmentActivity activity, View contentView, int width, int height) {
		super(contentView, width, height, true);
		mContentView = contentView;
		mActivity = activity;
		//		setBackgroundDrawable(new BitmapDrawable());
		setBackgroundDrawable(new ColorDrawable(0x00000000));
		backgroundAlpha(0.6f);
		setOnDismissListener(this);
		// 设置可以获取焦点
		setFocusable(true);
		// 设置可以触摸弹出框以外的区域
		setOutsideTouchable(true);
		// 更新popupwindow的状态
		update();
		initViews();
		initEvents();
	}

	public BasePop(View contentView) {
		super(contentView);
	}

	public View findViewById(int id) {
		return mContentView.findViewById(id);
	}

	public abstract void initViews();

	public abstract void initEvents();

	public void backgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
		lp.alpha = bgAlpha;
		mActivity.getWindow().setAttributes(lp);
	}

	@Override
	public void onDismiss() {
		backgroundAlpha(1);
	}
}
